package com.guodu.dao.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.MaterialRukuDao;
import com.guodu.entity.MaterialRuku;
import com.guodu.entity.MaterialrukuStatus;
import com.guodu.entity.Units;
import com.guodu.vo.MaterialRukuQueryVo;

@Repository("materialRukuDao")
public class MaterialRukuDaoImpl extends BaseDao implements MaterialRukuDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialRuku> findByVo(MaterialRukuQueryVo materialRukuQueryVo,Units units,
			int curPage, int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialRuku m where 1=1");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append(" order by rkId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public int findSizeByVo(
			MaterialRukuQueryVo materialRukuQueryVo,Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from MaterialRuku m where 1=1");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		return super.findInt(hql.toString());
	}

	@Override
	public MaterialRuku findById(String id) {
		// TODO Auto-generated method stub
		return (MaterialRuku) super.findById(MaterialRuku.class, id);
	}

	@Override
	public void del(String id) {
		// TODO Auto-generated method stub
		super.delete(MaterialRuku.class, id);
	}

	@Override
	public void delByIds(String[] ids) {
		// TODO Auto-generated method stub
		super.deleteAllById(MaterialRuku.class, ids);
	}

	@Override
	public void save(MaterialRuku entity) {
		// TODO Auto-generated method stub
		entity.setRkId("1");
		entity.setCreateTime(Calendar.getInstance().getTime());
		super.save(entity);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialRuku set status =");
		hql.append(status);
		hql.append(" where rkId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void update(MaterialRuku entity) {
		// TODO Auto-generated method stub
		super.update(entity);
	}

	@Override
	public void changeStatus(List<MaterialrukuStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialrukuStatus> findStatus(String id) {
		// TODO Auto-generated method stub
		return super.findByProperty(MaterialrukuStatus.class, "materialId", id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MaterialRuku> findAllByVo(
			MaterialRukuQueryVo materialRukuQueryVo, Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from MaterialRuku m where 1=1");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append("order by createTime desc");
		return super.find(hql.toString());
	}

	@Override
	public BigDecimal findAllMoney(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select sum(money) from MaterialRuku m where 1=1");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append("order by createTime desc");
		return super.findDouble(hql.toString());
	}

	@Override
	public boolean examinAll(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update MaterialRuku m set status=1 where status=0");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+units.getUnitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	
	public boolean huituiAll(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		StringBuffer hql = new StringBuffer("update MaterialRuku m set status=2 where status=0");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+units.getUnitsId()+")");
		super.executeUpdate(hql.toString());
		return true;
	}
	@Override
	public boolean shanchuAll(MaterialRukuQueryVo materialRukuQueryVo,
			Units units) {
		StringBuffer hql = new StringBuffer("delete from  MaterialRuku m  where (status=0 or status=2) ");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append(" and status=1");
		}else{
			hql.append(" and unitsId="+units.getUnitsId());
		}
		super.executeUpdate(hql.toString());
		return true;
	}

	@Override
	public boolean allBack(MaterialRukuQueryVo materialRukuQueryVo, Units units) {
		StringBuffer hql = new StringBuffer("update MaterialRuku m set status=0 where status=1");
		if(materialRukuQueryVo!=null){
			if(materialRukuQueryVo.getBuyer()!=null&&materialRukuQueryVo.getBuyer().trim().length()>0){
				hql.append(" and m.buyer like '%"+materialRukuQueryVo.getBuyer()+"%'");
			}
			if(materialRukuQueryVo.getDrawingnum()!=null&&materialRukuQueryVo.getDrawingnum().trim().length()>0){
				hql.append(" and m.drawingnum like '%"+materialRukuQueryVo.getDrawingnum()+"%'");
			}
			if(materialRukuQueryVo.getFunitsId()!=null){
				hql.append(" and exists(select 1 from Units u where u.unitsId=m.unitsId and u.funitsId="+materialRukuQueryVo.getFunitsId()+")");
			}
			if(materialRukuQueryVo.getMaterialCode()!=null&&materialRukuQueryVo.getMaterialCode().trim().length()>0){
				hql.append(" and materialCode like '%"+materialRukuQueryVo.getMaterialCode()+"%'");
			}
			if(materialRukuQueryVo.getMaterialName()!=null&&materialRukuQueryVo.getMaterialName().trim().length()>0){
				hql.append(" and materialName like '%"+materialRukuQueryVo.getMaterialName()+"%'");
			}
			if(materialRukuQueryVo.getRkBeginDate()!=null&&materialRukuQueryVo.getRkBeginDate().trim().length()>0){
				hql.append(" and rkTime >= TO_DATE('"+materialRukuQueryVo.getRkBeginDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getRkEndDate()!=null&&materialRukuQueryVo.getRkEndDate().trim().length()>0){
				hql.append(" and rkTime <= TO_DATE('"+materialRukuQueryVo.getRkEndDate()+"','yyyy-mm-dd')");
			}
			if(materialRukuQueryVo.getSpecifications()!=null&&materialRukuQueryVo.getSpecifications().trim().length()>0){
				hql.append(" and specification like '%"+materialRukuQueryVo.getSpecifications()+"%'");
			}
			if(materialRukuQueryVo.getSupplier()!=null&&materialRukuQueryVo.getSupplier().trim().length()>0){
				hql.append(" and supplier like '%"+materialRukuQueryVo.getSupplier()+"%'");
			}
			if(materialRukuQueryVo.getTexture()!=null&&materialRukuQueryVo.getTexture().trim().length()>0){
				hql.append(" and texture like '%"+materialRukuQueryVo.getTexture()+"%'");
			}
			if(materialRukuQueryVo.getUnitsId()!=null){
				hql.append(" and unitsId="+materialRukuQueryVo.getUnitsId());
			}
			if(materialRukuQueryVo.getCreateBeginTime()!=null&&materialRukuQueryVo.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+materialRukuQueryVo.getCreateBeginTime()+ " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getCreateEndTime()!=null&&materialRukuQueryVo.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+materialRukuQueryVo.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(materialRukuQueryVo.getStatus()!=null&&materialRukuQueryVo.getStatus().trim().length()>0&&!materialRukuQueryVo.getStatus().equals("5")){
				hql.append(" and status ="+materialRukuQueryVo.getStatus());
			}
		}
		if(units==null){
			hql.append("and status=1");
		}
		super.executeUpdate(hql.toString());
		return true;
	}

}
